package com.heima.config;

import com.heima.model.constants.RabbitMQConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitMQ延时配置类
 * @author chenshan
 */
@Configuration
public class RabbitMQConfig {

    /**
     * 延时交换机，交换机类型为CustomExchange
     * @return CustomExchange
     */
    @Bean
    public CustomExchange customExchange() {
        Map<String, Object> args = new HashMap<>();
        //延时交换机一定要设置x-delayed-type属性  
        args.put("x-delayed-type", "direct");  
        //第二个参数type需要设置成x-delayed-message  
        return new CustomExchange(RabbitMQConstants.RABBITMQ_DELAY_EXCHANGE_NAME, RabbitMQConstants.RABBITMQ_DELAY_EXCHANGE_TYPE, true, false, args);
    }


    /**
     * 延时队列
     * @return Queue
     */
    @Bean
    public Queue delayQueue() {
        return new Queue(RabbitMQConstants.RABBITMQ_DELAY_QUEUE, true);
    }

    /**
     * 延时队列绑定交换机
     * @return Binding
     */
    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(delayQueue()).to(customExchange()).with(RabbitMQConstants.RABBITMQ_DELAY_KEY).noargs();
    }  
      
}
